package LineCode.剑指Offer.代码的鲁棒性;

/**
 * 输入一个链表，输出该链表中倒数第k个结点。
 */
public class 链表中倒数第k个节点 {

    public static void main(String[] args) {

    }

    public ListNode FindKthToTail(ListNode head,int k) {
        ListNode temp = head, pre = head;
        int length = 0;
        int j = k;
        while (temp != null) {
            length++;
            temp = temp.next;
            if (j<1)
                pre = pre.next;
            j--;
        }
        if (length < k) return null;
        return pre;
    }

    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
}
